package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author alva
 * @since 2024-11-25
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    /**
     * 分页查询服务列表
     * @param servePageQueryReqDTO
     * @return
     */
    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper regionMapper;
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {

        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
    }
    /**
     * 新增服务
     * @param serveUpsertReqDTOList
     */

    @Override
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        //遍历serveUpsertReqDTOList
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            //合法校验
            //serve_item是否启用
            Long serveItemId = serveUpsertReqDTO.getServeItemId();
            ServeItem serveItem = serveItemMapper.selectById(serveItemId);
            if (ObjectUtil.isNull(serveItem) || serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                //抛出异常
                throw new ForbiddenOperationException("服务项不存在或未启用,不允许添加");
            }
            //同一个区域下不能添加相同的服务
            Integer count = lambdaQuery()
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .count();
            if (count>0){
                throw new ForbiddenOperationException("同一个区域下不能添加相同的服务");
            }
            //属性拷贝
            Serve serve = BeanUtils.toBean(serveUpsertReqDTO, Serve.class);
            //查询区域
            Long regionId = serveUpsertReqDTO.getRegionId();
            Region region = regionMapper.selectById(regionId);
            //设置城市编码
            String cityCode = region.getCityCode();

            serve.setCityCode(cityCode);
            //向serve插入数据
            baseMapper.insert(serve);



        }
    }

    @Override
    public Serve update(Long id, BigDecimal price) {
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!update){
            throw new CommonException("修改服务价格识别");
        }
        //查询serve数据
        Serve serve = baseMapper.selectById(id);

        return serve;
    }

    @Override
    public Serve onSale(Long id) {

        //根据id查询serve信息
        Serve serve = baseMapper.selectById(id);
        if (ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("区域服务信息不存在");
        }
        //如果serve的sale_status是0或1可以上架
        Integer saleStatus = serve.getSaleStatus();
        if (!(saleStatus==FoundationStatusEnum.INIT.getStatus()||saleStatus==FoundationStatusEnum.DISABLE.getStatus())){
            throw new ForbiddenOperationException("服务状态不正确,不能上架");
        }
        //如果serve_item没有启用不能上架
        Long serveItemId = serve.getServeItemId();
        ServeItem serveItem = serveItemMapper.selectById(serveItemId);
        Integer activeStatus = serveItem.getActiveStatus();
        if (!(activeStatus==FoundationStatusEnum.ENABLE.getStatus())){
            throw new ForbiddenOperationException("服务项未启用,不能上架");
        }
        //更新sale_status
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!update){
            throw new CommonException("上架失败");
        }
        return baseMapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        //根据id查询serve信息
        Serve serve = baseMapper.selectById(id);
        //如果serve的sale_status是0可以删除
        Integer saleStatus = serve.getSaleStatus();
        if (!(saleStatus==FoundationStatusEnum.INIT.getStatus())){
            throw new ForbiddenOperationException("服务状态不正确,不能删除");
        }
        //如果serve_item启用不能删除
        Long serveItemId = serve.getServeItemId();
        ServeItem serveItem = serveItemMapper.selectById(serveItemId);
        Integer activeStatus = serveItem.getActiveStatus();
        if ((activeStatus==FoundationStatusEnum.ENABLE.getStatus())){
            throw new ForbiddenOperationException("服务项已启用,不能删除");
        }
        //删除
        baseMapper.deleteById(id);
    }

    @Override
    public Serve offSale(Long id) {
        //根据id查询serve信息
        Serve serve = baseMapper.selectById(id);
        if (ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("区域服务信息不存在");
        }
        //2.校验服务状态是否为上架状态
        if(serve.getSaleStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务状态不为上架状态,无法下架");
        }
        //更新sale_status
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!update){
            throw new CommonException("下架失败");
        }
        return baseMapper.selectById(id);
    }

}
